package sc;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class OpenURL
{
	private static String ventuser = null;
	private static String ventpass = null;

	@SuppressWarnings("unchecked")
	public static void openURL(String url)
	{
		String osName = System.getProperty("os.name");
		try
		{
			if (osName.startsWith("Mac OS"))
			{
				Class fileMgr = Class.forName("com.apple.eio.FileManager");
				Method openURL = fileMgr.getDeclaredMethod("openURL",
						new Class[]
						{ String.class });
				openURL.invoke(null, new Object[]
				{ url });
			} else if (osName.startsWith("Windows"))
				Runtime.getRuntime().exec(
						"rundll32 url.dll,FileProtocolHandler " + url);
			else
			{
				// assume Unix or Linux
				String[] browsers =
				{ "firefox", "opera", "konqueror", "epiphany", "mozilla",
						"netscape" };
				String browser = null;
				for (int count = 0; count < browsers.length && browser == null; count++)
					if (Runtime.getRuntime().exec(new String[]
					{ "which", browsers[count] }).waitFor() == 0)
						browser = browsers[count];
				if (browser == null)
					throw new Exception("Could not find web browser");
				else
					Runtime.getRuntime().exec(new String[]
					{ browser, url });
			}
		} catch (Exception e)
		{
			Log.info(e.getLocalizedMessage());
		}
	}

	public static void openForum()
	{
		openURL("http://www.theharlequin.com.au/forum/");
	}

	public static void joinChat()
	{
		openURL("http://www.theharlequin.com.au/forum/hchat/flashchat.php");
	}

	public static void setVentCredentials(String ventuser, String ventpass)
	{
		OpenURL.ventuser = ventuser;
		OpenURL.ventpass = ventpass;
	}

	public static void joinVent()
	{
		openURL("ventrilo://harlequin.servegame.com:4328/servername=\"Harlequin%20Vent\"&serverpassword="
				+ ventpass);
	}

	public static void openProfile(String member_id)
	{
		openURL("http://www.theharlequin.com.au/forum/index.php?action=profile;u="
				+ member_id);
	}

	public static void openSendMessage(String member_id)
	{
		openURL("http://www.theharlequin.com.au/forum/index.php?action=pm;sa=send;u="
				+ member_id);
	}

	public static void postPM(String from, String to, String subject,
			String message)
	{
		try
		{
			String url = "http://www.theharlequin.com.au/sysbox/test/pm.php";
			URL pmurl = new URL(url);
			URLConnection conn = pmurl.openConnection();
			// Let the run-time system (RTS) know that we want input.
			conn.setDoInput(true);
			// Let the RTS know that we want to do output.
			conn.setDoOutput(true);
			// No caching, we want the real thing.
			conn.setUseCaches(false);
			// Specify the content type.
			conn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			// Send POST output.
			DataOutputStream out = new DataOutputStream(conn.getOutputStream());
			String content = "u=" + URLEncoder.encode(from, "UTF-8") + "&t="
					+ URLEncoder.encode(to, "UTF-8") + "&s="
					+ URLEncoder.encode(subject, "UTF-8") + "&m="
					+ URLEncoder.encode(message, "UTF-8");
			out.writeBytes(content);
			out.flush();
			out.close();
			// Get response data.
			DataInputStream in = new DataInputStream(conn.getInputStream());
			in.read();
			in.close();
		} catch (Exception ex)
		{
			Log.info(ex.getLocalizedMessage());
		}
	}
}
